<template>
    <div class="login_body">
        <!-- 账号密码登录 -->
        <div class="login_box" v-if="loginSwitch">
            <!-- 登录的第一行 -->
            <div class="login_title">
                <p>登录</p>
                <router-link to="/register">免费注册</router-link>
                <div class="qrCode">
                    <img class="login_switch" src="../../../assets/img/login_switch_01.png" alt="" @click="loginSwitch=false">
                </div>
            </div>
            <!-- ../../../assets/img/login_switch_01.png -->

            <!-- 登录的输入表单 -->
            <div class="login_form">
                <el-form
                    ref="ruleFormRef"
                    :model="ruleForm"
                    status-icon
                    :rules="rules"
                    label-width="auto"
                    label-position="left"
                    class="demo-ruleForm"
                >
                <!-- 输入手机号 -->
                    <el-form-item label="手机号" prop="mobile">
                        <el-input v-model="ruleForm.mobile"   placeholder="请输入您的手机号"/>
                    </el-form-item>
                    <!-- 输入密码 -->
                    <el-form-item label="密码" prop="password">
                        <el-input v-model="ruleForm.password"  type="password"  placeholder="请输入您的密码"/>
                    </el-form-item>
                    <!-- 是否记住用户名 -->
                    <el-checkbox v-model="checked" label="记住用户名" size="small" />
                    <!-- 忘记密码 -->
                    <div class="forget"><router-link to="/retrievePassword">忘记密码</router-link></div>
                    <!-- 登录按钮 -->
                    <el-form-item>
                        <el-button type="primary" @click="submitForm(ruleForm)"  color="#FF8094" >登 录</el-button>
                    </el-form-item>
                </el-form>
                <!-- 其他登录方式 -->
                <div class="login_other">
                    <span>其他登录方式</span>
                    <img src="@/assets/img/login_icon_weibo@3x.png" alt="微信登录">
                    <img src="@/assets/img/login_icon_weixin@3x.png" alt="微博登录">
                </div>
            </div>



            <div class="login_other"></div>
        </div>
        <!-- 切换成扫码登录 -->
        <div class="login_box" v-if="!loginSwitch">
            <div class="login_tips">
                <img src="@/assets/img/login_picture_01.png" alt="">
                打开<a href="">美呀APP</a>,扫一扫登录
                <img class="login_switch" src="../../../assets/img/login_switch_02.png" alt="" @click="loginSwitch=true">
            </div>
            <img class="login_qrCode" src="@/assets/img/login_picture_02.png" alt="扫描二维码登录">
            <p class="download">点击下载<a href="">美呀APP</a></p>
        </div>
    </div>
</template>

<script setup>
    import { reactive, ref,onMounted } from 'vue'
    import  useLoginStore  from '@/store/login'
    // import { ElNotification } from 'element-plus'
    import { Login } from '@/api'
    import { setLocalToken,setLocalPhone,getLocalPhone,clearPhone } from '@/utils/token'
    import { useRouter } from 'vue-router'
    const router = useRouter()
    const LoginStore = useLoginStore()
    //获取到表单组件
    let ruleFormRef = ref()
    let ruleForm = reactive({
        mobile: null,
        password: null,
    })
    // 定义是否禁用按钮
    // let btn_able = ref(true)
    // 双向数据绑定是否选中记住用户名
    let checked = ref(false)
    //switch用于切换账号密码登录还是扫描二维码登录
    let loginSwitch = ref(true)

    onMounted(() => {
        //判断之前是否记住账号决定是否显示手机号和勾选记住密码
        ruleForm.mobile = getLocalPhone() ? getLocalPhone() : null
        checked.value = getLocalPhone() ? true : false
    })


    // 登录功能
    const submitForm = async(ruleForm) =>{
        const res = await Login(ruleForm)
        console.log(res)
        if(res.code == 200){
            setLocalToken(res.user.token)
            LoginStore.setUserMessage(res.user.id,res.user.username,res.user.mobile);
            // 判断是否勾选记住密码
            if(!checked.value){
                clearPhone()
            }else{
                setLocalPhone(ruleForm.mobile)
            }
            router.replace('/home')
        }
    }


    //定义校验函数
    const validatePhone = (rule, value, callback) => {
        if (!value) {
            callback(new Error('手机号不能为空'))
        } else if(!LoginStore.reg_phone.test(value)) {
            callback(new Error('请输入正确的手机号'))
        }else {
            callback()
        }
    }
     // 校验密码
    const validatePass = (rule, value, callback) => {
        if (!value) {
            callback(new Error('密码不能为空'))
        } else if(!LoginStore.reg_password.test(value)) {
            callback(new Error('请重新输入6~16位数字加字母的密码'))
        }else {
            callback()
        }
    }
    //定义校验规则
    const rules = reactive({
        mobile: [{ validator: validatePhone, trigger: 'blur' }],
        password: [{ validator: validatePass, trigger: 'blur' }],
    })

</script>

<style scoped>
.login_body{
    width: 100%;
    height: 600px;
    background-image: url('@/assets/img/background4.png');
    background-size: 980px 600px;
    background-repeat:no-repeat;
    background-position: center;
    background-color: #fbf7f8;
}
.login_box{
    width: 460px;
    height: 370px;
    background-color:#fefefe;
    float: right;
    margin-right: 250px;
    margin-top: 50px;
    opacity:0.8;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    position: relative;
}
.login_title{
    width: 400px;
    height: 60px;
    line-height: 60px;
}
.login_title>p{
    font-size: 22px;
    font-weight: 500;
    float: left;
}
.login_title>a{
    font-size: 14px;
    color: #ff8aa5;
    float: left;
    margin-left: 20px;
}
.login_switch{
    width: 32px;
    position: absolute;
    top: 10px;
    right: 10px;
    cursor: pointer;
}
.login_form{
    width: 400px;
}
/* 输入框样式 */
:deep(.el-input__wrapper){
    height: 50px;
}
:deep(.el-form-item__label){
    height: 50px;
    line-height: 50px;
    font-size: 14px;
    color: #333;
}
/* 按钮样式 */
:deep(.el-button){
    width: 400px;
    height: 60px;
    color: #fff;
    font-size: 22px;
    margin-top: 10px;
}
.forget{
    font-size: 12px;
    float: right;
    margin-top: 6px;
    cursor:pointer;
}
.login_other>span{
    font-size: 14px;
    color: #8b8987;
    cursor:pointer;
}
.login_other>img{
    width: 24px;
    float: right;
    margin-left: 10px;
    cursor:pointer;
}
.login_tips{
    width: 300px;
    height: 50px;
    text-align: center;
    line-height: 50px;
    margin-bottom: 10px;
}
.login_tips>img:first-child{
    margin-right: 10px;
}
.download{
    font-size: 14px;
    margin-top: 20px;
}
.login_box a{
    color: #ff7193;
}
</style>